package application.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
import com.jfinal.kit.StrKit;

import application.common.model.Store;
import application.common.render.HttpClientResult;
import net.sf.json.JSONObject;

public class StoreUtil {

	public static void pull(String storeId) {
	    ExecutorService executor = Executors.newFixedThreadPool(2);
	    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
	        @Override
	        public Integer get() {
	            System.out.println("===task start===");
	            try {
	              
	            	if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
						Map<String, String> params =new HashMap<String,String>();
						params.put("storeId",storeId);
						params.put("cabinetId", ShiroUtil.getAuth());
	            		HttpClientResult httpClientResult=HttpUtil.doPost(String.format("%s/api/cab/store/getStoreById", ShiroUtil.getConfig().getSynURL()), params);
	            		
						if(httpClientResult==null) {
							throw new Exception("无响应.");
						}
						if(httpClientResult.getCode()!=200) {
							throw new Exception("异常："+httpClientResult.getCode());
						}
						String content=httpClientResult.getContent();
						if(StrKit.isBlank(content)) {
							throw new Exception("未返回信息.");
						}
						JSONObject jsonObject=JSONObject.fromObject(content);
						if(jsonObject==null) {
							throw new Exception("未返回信息.");
						}
						
						if(!jsonObject.has("code")) {
							throw new Exception("数据缺失.");
						}
						
						int code=jsonObject.getInt("code");
						if(code!=200) {
							throw new Exception(jsonObject.getString("message"));
						}
						
						if(!jsonObject.has("data")) {
							throw new Exception("数据缺失，无data.");
						}
						
						String data=jsonObject.getString("data");
						if(StrKit.isBlank(data)) {
							throw new Exception("数据缺失，无data.");
						}
						
						JSONObject object=JSONObject.fromObject(data);
						if(object==null) {
							throw new Exception("data is null.");
						}
						
						if(object.containsKey("id")) {
							Store store=Store.dao.findById(object.getString("id"));
							if(store!=null) {
								if(object.containsKey("field1") && !StrKit.equals(object.getString("field1"), store.getField1())) {
									store.setField1(object.getString("field1"));
								}
								if(object.containsKey("field2")&& !StrKit.equals(object.getString("field2"), store.getField2())) {
									store.setField2(object.getString("field2"));
								}
								if(object.containsKey("field3")&& !StrKit.equals(object.getString("field3"), store.getField3())) {
									store.setField3(object.getString("field3"));
								}
								if(object.containsKey("field4")&& !StrKit.equals(object.getString("field4"), store.getField4())) {
									store.setField4(object.getString("field4"));
								}
								if(object.containsKey("field5")&& !StrKit.equals(object.getString("field5"), store.getField5())) {
									store.setField5(object.getString("field5"));
								}
								if(object.containsKey("status")&& !StrKit.equals(object.getString("status"), store.getStatus())) {
									store.setStatus(object.getString("status"));
								}
								if(ShiroUtil.getConfig()!=null && StrKit.equals(ShiroUtil.getConfig().getState(),"0")) {
									store.update();
								}
							}
						}
	            	}
	            } catch (InterruptedException e) {
	            } catch (Exception e) {
				}
	            return 3;
	        }
	    }, executor);
	    future.thenAccept(e -> System.out.println(e));
	}
	
}
